﻿using System;
using System.Diagnostics;
using System.Windows;
using Caliburn.Micro;
using Microsoft.Phone.Applications.Common;

namespace ScreenRotateSample.ViewModels
{
    public class MainViewModel : Conductor<IScreen>.Collection.OneActive
    {
        private readonly INavigationService navigationService;

        private readonly Page1ViewModel item1;
        private readonly Page2ViewModel item2;

        private string Appname;

        public string AppName
        {
            get { return Appname; }
            set
            {
                Appname = value;
                NotifyOfPropertyChange(() => AppName);
            }
        }

        public MainViewModel(INavigationService navigationService, Page1ViewModel item1, Page2ViewModel item2)
        {
            this.navigationService = navigationService;
            this.item1 = item1;
            this.item2 = item2;

            AccelerometerHelper.Instance.IsActive = true;
            if (AccelerometerHelper.Instance.NoAccelerometer)
            {
                System.Diagnostics.Debug.WriteLine("Accelerometer Not Found!");
            }
            else
            {
                DeviceOrientationHelper.Instance.OrientationChanged += new EventHandler<DeviceOrientationChangedEventArgs>(orientationHelper_OrientationChanged);
            }
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            Items.Add(item1);
            Items.Add(item2);

            ActivateItem(item1);
        }

        public void Report()
        {
            //IoC.Get<ExtractViewModel>().ListTransaction = null;
            //navigationService.UriFor<ExtractViewModel>().Navigate();
            MessageBox.Show("Report");
        }

        private void orientationHelper_OrientationChanged(object sender, DeviceOrientationChangedEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => ChangeOrientation(e));
        }

        private void ChangeOrientation(DeviceOrientationChangedEventArgs e)
        {
            if (e.CurrentOrientation == DeviceOrientation.PortraitRightSideUp) //PortraitRightSideUp
            {
                MessageBox.Show("Up");
            }
            else if (e.CurrentOrientation == DeviceOrientation.PortraitUpSideDown) //PortraitUpSideDown
            {
                MessageBox.Show("Down");
            }
        }
    }
}
